<?php
/**
 * 采购退料单
 * Created by PhpStorm.
 * User: echa
 * Date: 2018/1/26
 * Time: 21:49
 *
 */
namespace App\Services\Warehouse;

use App\Http\Controllers\ApiController;

class PurchaseReturnSlipService extends PublicService {

    //采购退料表单ID
    //protected  $Formid='PUR_MRB';

    public function __construct()
    {
        parent::__construct(config('k3cloud.PurchaseReturnSlip'));
    }

    /**
     *查看
     *1.formid：业务对象表单Id（必录）
    2.data：Json格式数据（详情参考Json格式数据）（必录）
    2.1.CreateOrgId：创建者组织，字符串类型（非必录）
    2.2.Number：单据编号，字符串类型（用编号查询必录）
    2.3.Id：表的主键Id（用Id查询时必录）
     *
     */
    public  function ViewPurchaseReturnSlip($Number='',$Id=''){
        return $this->view($Number);
    }


    /**
    保存
     * 1.formid：业务对象表单Id（必录）
    2.data：Json格式数据（详情参考Json格式数据）（必录）
    2.1.Creator：创建者名字（非必录）
    2.2.NeedUpDateFields：需要保存的字段,格式["fieldkey1","fieldkey2","entitykey1",...]，数组类型(非必录)
    2.3.NeedReturnFields：需要返回的结果字段,格式["fieldkey","entitykey.fieldkey",...]（非必录）
    2.4.IsDeleteEntry：是否删除分录，默认True删除（非必录）
    2.5.SubSystemId：菜单所在子系统Id（非必录）
    2.6.IsVerifyBaseDataField：是否验证所有的基础资料，默认False不验证（非必录）
    2.7.IsEntryBatchFill：是否批量填充分录：默认为Ture（非必录）
    2.8.Model：表单实体数据（必录）
     */
    public function SavePurchaseReturnSlip($arrdata=[]){
        if(empty($arrdata['FID'])){
            $arrdata['FID']="0";
        }
        if(empty($arrdata['FBillNo'])){
            $arrdata['FBillNo']="";
        }
        $goods=$this->PostGoodsData($arrdata['GoodsData']);
        $data=[
            "FID"=>$arrdata['FID'],
            "FBillTypeID"=>[
                "FNumber"=>"TLD01_SYS"
            ],
            "FBillNo"=>$arrdata['FBillNo'],
            "FDate"=>date('Y-m-d H:i:s',time()),
            "FMRTYPE"=>"B",
            "FMRMODE"=>"A",
            "FStockOrgId"=>[
                "FNumber"=>config('k3cloud.organization')
            ],
            "FMRDeptId"=>[
                "FNumber"=>""
            ],
            "FSTOCKERGROUPID"=>[
                "FNumber"=>""
            ],
            "FCorrespondOrgId"=>[
                "FNumber"=>""
            ],
            "FSTOCKERID"=>[
                "FNumber"=>""
            ],
            "FMRREASON"=>[
                "FNumber"=>""
            ],
            "FRequireOrgId"=>[
                "FNumber"=>config('k3cloud.organization')
            ],
            "FPurchaseOrgId"=>[
                "FNumber"=>config('k3cloud.organization')
            ],
            "FPURCHASEDEPTID"=>[
                "FNumber"=>""
            ],
            "FPURCHASERGROUPID"=>[
                "FNumber"=>""
            ],
            "FPURCHASERID"=>[
                "FNumber"=>""
            ],
            "FSupplierID"=>[
                "FNumber"=>$arrdata['k3_number']
            ],
            "FDESCRIPTION"=>"",
            "FACCEPTORID"=>[
                "FNumber"=>$arrdata['k3_number']
            ],
            "FAcceptAddress"=>"",
            "FSettleId"=>[
                "FNumber"=>$arrdata['k3_number']
            ],
            "FCHARGEID"=>[
                "FNumber"=>$arrdata['k3_number']
            ],
            "FOwnerTypeIdHead"=>"BD_OwnerOrg",
            "FOwnerIdHead"=>[
                "FNumber"=>config('k3cloud.organization')
            ],
            "FScanPoint"=>[
                "FNumber"=>""
            ],
            "FConfirmerId"=>[
                "FUserID"=>""
            ],
            "FConfirmDate"=>"1900-01-01",
            "FScanBox"=>"",
            "FCDateOffsetUnit"=>"",
            "FCDateOffsetValue"=>0,
            "FAcceptorContactID"=>[
                "FCONTACTNUMBER"=>""
            ],
            "FPURMRBFIN"=>[
                "FSettleOrgId"=>[
                    "FNumber"=>config('k3cloud.organization')
                ],
                "FSETTLETYPEID"=>[
                    "FNumber"=>""
                ],
                "FSettleCurrId"=>[
                    "FNumber"=>"PRE001"
                ],
                "FPAYCONDITIONID"=>[
                    "FNumber"=>""
                ],
                "FIsIncludedTax"=>true,
                "FPRICETIMEPOINT"=>"1",
                "FPRICELISTID"=>[
                    "FNumber"=>"CGJM000064"
                ],
                "FDISCOUNTLISTID"=>[
                    "FNumber"=>""
                ],
                "FLOCALCURRID"=>[
                    "FNumber"=>"PRE001"
                ],
                "FEXCHANGETYPEID"=>[
                    "FNumber"=>"HLTX01_SYS"
                ],
                "FEXCHANGERATE"=>1.0,
                "FISPRICEEXCLUDETAX"=>true
            ],
            "FPURMRBENTRY"=>$goods
        ];
        return $this->save($data);

    }

    /**
    提交
     *1.formid：业务对象表单Id（必录）
    2.data：Json格式数据（详情参考Json格式数据）（必录）
    2.1.CreateOrgId：创建者组织，字符串类型（非必录）
    2.2.Numbers：单据编号集合，格式["billNo1","billNo1",...]，数组类型（必录）
     */
    public function  SubmitPurchaseReturnSlip($Numbers=[]){
        return $this->submit($Numbers);
    }

    /**
    审核
     *
     * 1.formid：业务对象表单Id（必录）
    2.data：Json格式数据（详情参考Json格式数据）（必录）
    2.1.CreateOrgId：创建者组织，字符串类型（非必录）
    2.2.Numbers：单据编号集合，格式["billNo1","billNo1",...]，数组类型（必录）

     */
    public function AuditPurchaseReturnSlip($Numbers=[]){
        return $this->audit($Numbers);
    }

    /**
     *单据列表查询
     * 1.data：Json格式数据（详情参考Json格式数据）（必录）
    1.1.FormId：业务对象表单Id（必录）   FMATERIALID,FMaterialName
    1.2.FieldKeys：字段keys，字符串类型用逗号分隔，比如"key1,key2..."（必录）
    1.3.FilterString：过滤（非必录）  FBillNo='CGTL000004'
    1.4.OrderString：排序字段（非必录）
    1.5.TopRowCount：总行数（非必录）
    1.6.StartRow：开始行（非必录）
    1.7.Limit：最大行数，不能超过2000（非必录）
     */
    public function ExecuteBillQueryPurchaseReturnSlip($FieldKeys='',$FilterString='',$OrderString='',$TopRowCount=0,$StartRow=0,$Limit=20)
    {
        if (empty($FieldKeys)) {
            $FieldKeys='FBillNo,FDate,FSupplierID,FCreatorId,FCreateDate';
        }
        return  $this->ExecuteBillQuery($FieldKeys,$FilterString,$OrderString,$TopRowCount,$StartRow,$Limit);
    }

    private function PostGoodsData($GoodsData){
        if (!is_array($GoodsData)) {
            return false;
        }
        $goods=array();
        foreach ($GoodsData as $k=>$v){
            $goods[]=[
                "FEntryID"=>null,
                "FRowType"=>"Standard",
                "FMATERIALID"=>[
                    "FNumber"=>$v['goods_sn']
                ],
                "FMaterialDesc"=>$v['goods_name'],
                "FParentMatId"=>[
                    "FNumber"=>""
                ],
                "FProjectNo"=>null,
                "FUnitID"=>[
                    "FNumber"=>$v['spec_unit']
                ],
                "FREPLENISHQTY"=>$v['goods_number'],
                "FKEAPAMTQTY"=>$v['goods_number'],
                "FPRICEUNITID"=>[
                    "FNumber"=>$v['spec_unit']
                ],
                "FSTOCKID"=>[
                    "FNumber"=>""
                ],
                "FStockStatusId"=>[
                    "FNumber"=>""
                ],
                "FNOTE"=>null,
                "FTaxCombination"=>[
                    "FNumber"=>""
                ],
                "FPrice"=>0.0,
                "FExtAuxUnitId"=>[
                    "FNumber"=>""
                ],
                "FExtAuxUnitQty"=>0.0,
                "FREQTRACENO"=>null,
                "FIsReceiveUpdateStock"=>false,
                "FInvoicedJoinQty"=>0.0,
                "FGiveAway"=>false,
                "FPriceBaseQty"=>$v['goods_number'],
                "FSetPriceUnitID"=>[
                    "FNumber"=>""
                ],
                "FCarryUnitId"=>[
                    "FNumber"=>$v['spec_unit']
                ],
                "FCarryQty"=>$v['goods_number'],
                "FCarryBaseQty"=>$v['goods_number'],
                "FPOORDERENTRYID"=>0,
                "FBILLINGCLOSE"=>false,
                "FPriceListEntry"=>[
                    "FNumber"=>""
                ],
                "FRMMUSTQTY"=>0.0,
                "FBOMID"=>[
                    "FNumber"=>""
                ],
                "FSupplierLot"=>null,
                "FLot"=>[
                    "FNumber"=>""
                ],
                "FProduceDate"=>null,
                "FEXPIRYDATE"=>null,
                "FAUXUNITQTY"=>0.0,
                "FENTRYTAXRATE"=>17.00,
                "FDISCOUNTRATE"=>0.0,
                "FTAXPRICE"=>0.0,
                "FRMREALQTY"=>$v['goods_number'],
                "FIsStock"=>false,
                "FChargeProjectID"=>[
                    "FNumber"=>""
                ],
                "FMtoNo"=>null
            ];
        }
        return $goods;
    }
}